package com.cloud.drore.eboss.supplier.controller;

import com.cloud.drore.eboos.common.base.PagePo;
import com.cloud.drore.eboos.common.base.ResultMessage;
import com.cloud.drore.eboos.common.entity.dealer.*;
import com.cloud.drore.eboos.common.util.LocalData;
import com.cloud.drore.eboos.exception.BusinessException;
import com.cloud.drore.eboss.supplier.entity.po.SupplierAndDistributorRelationPo;
import com.cloud.drore.eboss.supplier.operationApiService.ISupplierAndDistributorRelationService;
import com.cloud.drore.eboss.supplier.service.IBaseDataFeign;
import com.cloud.drore.eboss.supplier.service.IRefundOrderFeign;
import com.cloud.drore.eboss.supplier.service.impl.SupplierRefundOrderServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 浙江卓锐科技股份有限公司 版权所有  Copyright 2018<br>
 *
 * @Description: <br>
 * @Project: eboos <br>
 * @CreateDate: Created in 2018/2/2 11:04 <br>
 * @Author: <a href="575842778@qq.com">郑永涛</a>
 */
@RestController
@RequestMapping("/supplierRefundOrder")
@Api(value = "/供应商退订订单查询接口", description = "供应商退订订单查询接口-郑永涛")
public class SupplierRefundOrderController {

    private static String ORDER_STATUS = "ORDER_STATUS";

    @Resource
    IRefundOrderFeign refundOrderFeign;
    @Resource
    SupplierRefundOrderServiceImpl supplierRefundOrderService;

    @Resource
    ISupplierAndDistributorRelationService supplierAndDistributorRelationService;

    @Resource
    IBaseDataFeign baseDataFeign;

    @ApiOperation(value = "供应商条件查询退订订单")
    @PostMapping(value = "refundOrder/supplierQueryRefundOrder")
    public ResultMessage<List<SupplierRefundOrderQueryVo>> supplierQueryRefundOrder(@RequestBody PagePo<SupplierRefundOrderQueryPo> pagePo) throws BusinessException {
        // 当前供应商id
        pagePo.getFields().setSupplierId(LocalData.getCurrentUser().getTypeId());
        return refundOrderFeign.supplierQueryRefundOrder(pagePo);
    }

    @ApiOperation(value = "退订订单详情")
    @GetMapping(value = "refundOrder/{id}/{productType}")
    public ResultMessage<OrderDetailVo> get(@PathVariable("id") Long id, @PathVariable("productType") String productType) throws BusinessException {
        return refundOrderFeign.get(id,productType);
    }

    @ApiOperation(value = "供应商退订订单页面加载初始化信息")
    @GetMapping(value = "/supplierInit")
    public ResultMessage<RefundOrderDicVo> supplierInit() {
        Long supplierId = LocalData.getCurrentUser().getTypeId();
        List<SupplierAndDistributorRelationPo> data = supplierAndDistributorRelationService.getAllDistributorBySupplierId(supplierId).getData();
        List<DistributorVo> distributorList = data.stream().map(supplierAndDistributorRelationPo -> {
            DistributorVo distributorVo = new DistributorVo();
            distributorVo.setId(supplierAndDistributorRelationPo.getId());
            distributorVo.setMerchantName(supplierAndDistributorRelationPo.getDistributorName());
            return distributorVo;
        }).collect(Collectors.toList());
        RefundOrderDicVo refundOrderDicVo = new RefundOrderDicVo();
        refundOrderDicVo.setDistributorList(distributorList);
        refundOrderDicVo.setOrderStatus(baseDataFeign.getDicListMapByCodeVo(ORDER_STATUS));
        return new ResultMessage<>(refundOrderDicVo);
    }

    @ApiOperation(value = "运营商退订订单页面加载初始化信息")
    @GetMapping(value = "/operationInit")
    public ResultMessage<RefundOrderDicVo> operationInit() {
        return refundOrderFeign.operationInit();
    }

}